package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysCreditbankRemoteService;
import com.flyme.module.base.client.dto.SysUserDto;
import com.flyme.module.base.client.entity.SysCreditbank;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.service.SysCreditbankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 学分转换管理
 *
 * @author zyf
 */
@Api(tags = "学分转换管理")
@RestController
public class SysCreditbankController extends BaseController<SysCreditbankService, SysCreditbank> implements SysCreditbankRemoteService {


    /**
     * 学分转换列表
     */
    @ApiOperation(value = "学分转换列表", notes = "学分转换列表")
    public ResultBody list(@ModelAttribute PagerInfo pagerInfo, @ModelAttribute SysUserDto user) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("cb.*,user.userName,user.sex,user.mobile,user.birthday,user.address,user.email,user.company,user.deptName,user.positionName,user.qq,area.areaName areaName," +
            "(SELECT d.dicDataTitle FROM sys_dictdata d WHERE d.id = cb.nation) nationName," +
            "(SELECT d.dicDataTitle FROM sys_dictdata d WHERE d.id = cb.nationality) nationalityName," +
            "(SELECT d.dicDataTitle FROM sys_dictdata d WHERE d.id = cb.polStatus) polStatusName," +
            "(SELECT e.enumName FROM sys_enum e WHERE e.enumCode = cb.eduDegree) eduDegreeName");
        cq.createAlias("area", "area");
        cq.createAlias("user", "user");
        cq.ne("isPass", 3);
        cq.eq("isPass", user.getIsPass());
        cq.like("user.userName", user.getUserName());
        cq.like("user.mobile", user.getMobile());
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    /**
     * 学分详情
     */
    @ApiOperation(value = "学分转换详情", notes = "学分转换详情")
    public ResultBody details(@PathVariable Long userId) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(new PagerInfo());
        cq.select("cb.*,user.userName,user.sex,user.mobile,user.birthday,user.address,user.email,user.company,user.deptName,user.positionName,user.qq");
        cq.createAlias("area", "area");
        cq.createAlias("user", "user");
        cq.eq("cb.userId", userId);
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid.getRows().get(0));
    }

    /**
     * 审核
     */
    @ApiOperation(value = "学分审核", notes = "学分审核")
    public ResultBody opinion(@ModelAttribute SysCreditbank creditbank) {
        UpdateWrapper c = new UpdateWrapper();
        boolean tag = bizService.update(c.set(true, "creditScore", creditbank.getCreditScore())
            .set(true, "opinion", creditbank.getOpinion()).set(true, "isPass", creditbank.getIsPass()).eq(true, "id", creditbank.getId()));
        return ResultBody.ok("审核完成").setData(tag);
    }

}
